package team.stu.polygongame.method;
import java.util.Arrays;
import java.util.Objects;
import java.util.Random;

public class PolygonGeneration {

    Random rand = new Random();
//随机生成边和点的数量
    int num = rand.nextInt(7)+5;



//随机生成点的数据
    public String[] pointGeneration(int num){
        String[] point = new String[num];
        for(int i = 0; i < num; i++){
            point[i] = String.valueOf(rand.nextInt(15)-4);
        }
        return point;
    }
//随机生成边的数据
    public String[] vectorGeneration(int num){
        String[] vector = new String[num];
        for(int i = 0; i < num; i++){
            int j = rand.nextInt(2);
            if(j == 1){
                vector[i] = "+";
            }
            else vector[i] = "*";
        }
        return vector;
    }

//    public void vectorDelete(int i){
//
//        String[] newVector = new String[vector.length-1];
//
//        for(int k = 0; k < vector.length; k++){
//            if (k < i){
//                newVector[k] = vector[k];
//            }
//            else if (k > i){
//                newVector[k-1] = vector[k];
//            }
//        }
//        System.out.println(Arrays.toString(newVector));
//    }
//
//    public void pointCalculation(int i){
//
//        String[] newPoint = new String[point.length-1];
//
//        if(vector[i] == "+"){
//            newPoint[i] = String.valueOf(Integer.parseInt(point[i]) + Integer.parseInt(point[i+1]));
//        }
//        else newPoint[i] = String.valueOf(Integer.parseInt(point[i]) * Integer.parseInt(point[i+1]));
//        System.out.println(newPoint[i]);
//
//        for(int k = 0; k < point.length; k++){
//            if (k < i){
//                newPoint[k] = point[k];
//            }
//            if ((k > i)&&(k+1<point.length)){
//                newPoint[k] = point[k+1];
//            }
//        }
//        System.out.println(Arrays.toString(newPoint));
//    }

//测试用，将点和边的数据合并为一个先点后边的数组
    public String[] combine(String[] point, String[] vector){
        String[] data = new String[point.length+vector.length];
        for(int i = 0; i < data.length; i++){
            if(i%2 == 0){
                data[i] = point[i/2];
            }else if(i%2 == 1){
                data[i] = vector[i/2];
            }
        }
        System.out.println(Arrays.toString(data));
        return data;
    }


//将一个同时拥有边和点数据的数组分开，然后按照先边后点的方式排序
    public String[] divideAndRanking(String[] data){
        String[] pointData = new String[data.length/2];
        String[] vectorData = new String[data.length/2];
        int j = 0;
        int k = 0;
        for(int i = 0; i < data.length; i++){
            if((Objects.equals(data[i], "+"))||(Objects.equals(data[i], "*"))){
                vectorData[j] = String.valueOf(data[i]);
                j++;
            }else{
                pointData[k] = data[i];
                k++;
            }
        }

        String[] polygonList = new String[data.length];
        for(int i = 0; i < polygonList.length; i++){
            if(i%2 == 0){
                polygonList[i] = vectorData[i/2];
            }else if(i%2 == 1){
                if (i + 1 == polygonList.length){
                    polygonList[i] = pointData[0];
                } else {
                    polygonList[i] = pointData[i/2 + 1];
                }

            }
        }
        System.out.println(Arrays.toString(polygonList));
        return polygonList;
    }
}
